package org.csu.mypetstore.controller;

import com.sun.org.apache.xpath.internal.operations.Or;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONString;
import net.sf.json.JsonConfig;
import org.apache.ibatis.annotations.Delete;
import org.csu.mypetstore.domain.LineItem;
import org.csu.mypetstore.domain.Order;
import org.csu.mypetstore.domain.OrderStatus;
import org.csu.mypetstore.service.OrderService;
import org.csu.mypetstore.service.impl.OrderServiceImpl;
import org.csu.mypetstore.tools.JsonDateValueProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService = new OrderServiceImpl();

    @GetMapping
    @ResponseBody
    public String get(String type, @RequestParam(required = false,defaultValue = "") String id){
        System.out.println(id);

        if(type.equals("getOrder")){
            int orderid = Integer.valueOf(id);
            Order order = orderService.getOrder(orderid);
            JSONObject jsonObject = new JSONObject();
            JSONObject object= orderService.setJSON(order);
            jsonObject.put("status", "OK");
            jsonObject.put("msg","订单获取成功!");
            jsonObject.put("data", object);
            return jsonObject.toString();
        }
        else if (type.equals("getOrderListByUserID")){
            String userid = id;
            System.out.println(id);
            List<Order> orders = orderService.getOrderListByUserID(userid);
            JSONObject jsonObject = new JSONObject();

            jsonObject.put("status", "OK");
            jsonObject.put("msg","该用户所有订单获取成功!");
            jsonObject.put("data", Transfer.objectTransfer(orders));
            return jsonObject.toString();
        }
        else if (type.equals("getAllOrders")){
            List<Order> orders = orderService.getAllOrders();
            JSONArray jsonArray =new JSONArray();

            for(Order order:orders) {
                JSONObject jsonObject = orderService.setJSON(order);
                if(order.getOrderStatus().get(0).getStatus().equals("P"))jsonArray.add(jsonObject);
            }
            JSONObject jsonObject = new JSONObject();

            jsonObject.put("status", "OK");
            jsonObject.put("msg","所有订单获取成功!");
            jsonObject.put("data", jsonArray);
            return jsonObject.toString();
        }
        return "error";
    }

    @PostMapping
    @ResponseBody
    public String add(@RequestBody Order order){
        System.out.println(order.getOrderId());
        orderService.addOrder(order);
        System.out.println(order.toString());

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status", "OK");
        jsonObject.put("msg","新增订单成功!");
        return jsonObject.toString();
    }

    @PutMapping
    @ResponseBody
    public String update(String type,Order order,OrderStatus orderStatus){
        if(type.equals("order"))
        {
            orderService.updateOrder(order);
            JSONObject jsonObject = new JSONObject();

            jsonObject.put("status", "OK");
            jsonObject.put("msg","更新订单成功!");
            return jsonObject.toString();
        }
        else {
            orderService.updateOrderStatus(orderStatus);
            JSONObject jsonObject = new JSONObject();

            jsonObject.put("status", "OK");
            if(orderStatus.getStatus().equals("R")) jsonObject.put("msg", "退单成功!");
            else if(orderStatus.getStatus().equals("E")) jsonObject.put("msg", "发货成功!");
            return jsonObject.toString();
        }
    }

    @DeleteMapping
    @ResponseBody
    public String delete(int orderid){
        orderService.deleteOrder(orderid);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status", "OK");
        jsonObject.put("msg","删除订单成功!");
        return jsonObject.toString();
    }
}

class Transfer {
    public static<T> JSONArray objectTransfer(T t){
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor());
        return JSONArray.fromObject(t,jsonConfig);
    }
}
